package com.iplant.base.utils.log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.iplant.base.utils.BaseConfigConstans;
import com.iplant.base.utils.general.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iplant.base.po.BaseUser;
import com.iplant.base.po.ErrorLogItem;

import org.apache.commons.codec.Charsets;
import org.apache.commons.io.FileUtils;

public class ErrorLogTool {

	private static Logger logger = LoggerFactory.getLogger(ErrorLogTool.class);

	private static ErrorLogTool Instance = null;

	private ErrorLogTool() {
		super();
	}

	public static ErrorLogTool getInstance() {
		if (Instance == null)
			Instance = new ErrorLogTool();
		return Instance;
	}

	private static List<String> LogPathList = new ArrayList<String>();

	private synchronized static List<String> GetPathList() {

		try {
			if(LogPathList.size()>0)
				return LogPathList;

			if (StringUtils.isEmpty(BaseConfigConstans.ErrorLogConfigPath))
				return LogPathList;

			File wFile = new File(BaseConfigConstans.ErrorLogConfigPath);

			if (wFile.isDirectory()|| !wFile.isFile() || !wFile.exists()) {

				LogPathList.addAll(StringUtils.splitList( BaseConfigConstans.ErrorLogConfigPath,","));
				LogPathList.removeIf(p->StringUtils.isEmpty(p));
				if(LogPathList.size()<=0&&StringUtils.isNotEmpty(BaseConfigConstans.ErrorLogPath))
					LogPathList.add(BaseConfigConstans.ErrorLogPath);
				return LogPathList;
			}
			LogPathList.addAll(FileUtils.readLines(wFile,"utf-8"));
			LogPathList.removeIf(p->StringUtils.isEmpty(p));
			if(LogPathList.size()<=0&&StringUtils.isNotEmpty(BaseConfigConstans.ErrorLogPath))
				LogPathList.add(BaseConfigConstans.ErrorLogPath);

		} catch (Exception e) {
			logger.error(e.toString());
		}
		return LogPathList;
	}



	/**
	 * 获取服务所在的本机的错误日志目录
	 * 
	 * @return
	 */
	public List<ErrorLogItem> SelectList(BaseUser wLoginUser) {
		List<ErrorLogItem> wResult = new ArrayList<ErrorLogItem>();
		try {
			List<String> wCatalogList = GetPathList();


			if (wCatalogList == null || wCatalogList.size() == 0)
				return wResult;
			// 批量遍历删除
			for (String wItem : wCatalogList) {
				List<ErrorLogItem> wCoreLogList = GetErrorLogItemList(wLoginUser, wItem);
				if (wCoreLogList != null && wCoreLogList.size() > 0)
					wResult.addAll(wCoreLogList);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
		}
		return wResult;
	}

	/**
	 * 根据文件目录获取所有的文件以及文件夹
	 *
	 * @return
	 */
	private List<ErrorLogItem> GetErrorLogItemList(BaseUser wLoginUser, String wCatalog) {
		List<ErrorLogItem> wResult = new ArrayList<ErrorLogItem>();
		try {
			File wFile = new File(wCatalog);
			if (!wFile.isDirectory())
				return wResult;

			ErrorLogItem wFather = new ErrorLogItem();
			wFather.FileName = wFile.getName();
			wFather.IsDir = true;
			wFather.FilePath = wFile.getAbsolutePath();

			File[] wfss = wFile.listFiles();
			for (File wItem : wfss) {
				// 目录
				if (wItem.isDirectory()) {
					// 添加下级子目录
					wFather.SubLogList.addAll(GetErrorLogItemList(wLoginUser, wItem.getAbsolutePath()));
				} else {// 文件
					ErrorLogItem wSubCatalog = new ErrorLogItem();
					wSubCatalog.IsDir = false;
					wSubCatalog.FileName = wItem.getName();
					wSubCatalog.FilePath = wItem.getAbsolutePath();
					wFather.SubLogList.add(wSubCatalog);
				}
			}
			wResult.add(wFather);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
		}
		return wResult;
	}

	/**
	 * 根据ID查询某个日志文件的文本内容
	 *
	 * @return
	 */
	public List<String> SelectFile(BaseUser wLoginUser, String wFilePath) {
		List<String> wResult = new ArrayList<String>();
		try {

			if (StringUtils.isEmpty(wFilePath))
				return wResult;

			File wFile = new File(wFilePath);
			if (wFile.isDirectory())
				return wResult;

			if (wFile.exists())
				return FileUtils.readLines(wFile, Charsets.UTF_8);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
		}
		return wResult;
	}

	/**
	 * 根据ID删除日志文件
	 *
	 */
	public boolean DeleteFile(BaseUser wLoginUser, String wFilePath) {
		boolean wResult = true;
		try {
			if (StringUtils.isEmpty(wFilePath))
				return false;

			File wFile = new File(wFilePath);
			if (wFile.isDirectory())
				return false;
			if (wFile.exists())
				wFile.delete();
		} catch (Exception ex) {
			logger.error(ex.toString());
		}
		return wResult;
	}

	/**
	 * 删除文件集合
	 * 
	 * @param wFilePathList
	 * @return
	 */
	public String DeleteFileList(BaseUser wLoginUser, List<String> wFilePathList) {
		String wResult = "";
		try {
			if (wFilePathList == null || wFilePathList.size() == 0)
				wResult = "参数错误!";

			File wFile;
			for (String wFilePath : wFilePathList) {
				if (StringUtils.isEmpty(wFilePath))
					continue;

				wFile = new File(wFilePath);
				if (wFile.isDirectory())
					continue;
				if (wFile.exists())
					wFile.delete();

			}

		} catch (Exception e) {
			wResult = e.toString();
			logger.error(e.toString());
		}
		return wResult;
	}

}
